﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LX.Core.Utils;
using LX.Web.Mvc;
using S4Store.Service;
using S4Store.Domain;
using S4Store.Models;
using S4Store.Models.AfterSaleCarKPI;

namespace S4Store.Controllers
{
    public class AfterSaleCustomerServiceController : ReportBaseController
    {
        //
        // GET: /AfterSaleCustomerService/
        public S4Store.Service.EmployeeService EmployeeService { get; set; }
        public S4Store.Service.AfterSaleCarKPIService AfterSaleCarKPIService { get; set; }

        public ActionResult Index()
        {
            CustomerServiceIndexModel model = new CustomerServiceIndexModel();
            using (var t = new Transtion())
            {
                t.Inject(AfterSaleCarKPIService);
                t.Inject(EmployeeService);
                int[] employs = EmployeeService.GetEnableEmployee(Store.Id, EmployeeType.AccountSpecialist).ToList().Select(o => o.Id).ToArray();
                var data = AfterSaleCarKPIService.GetAfterSaleCustomerServiceDayInputQuery().Where(x => employs.Contains(x.EmployeeId)).ToList();
                model.MonthDayData = data;
                return View(model);
            }
        }

        public ActionResult DayInput(int y, int m,int d)
        {
            using (Transtion t = new Transtion())
            {
                t.Inject(EmployeeService);
                t.Inject(AfterSaleCarKPIService);
                CustomerServiceListModel model = new CustomerServiceListModel();
                model.Date = new DateTime(y, m, d);
                model.Employees = EmployeeService.GetEnableEmployee(Store.Id, EmployeeType.AccountSpecialist).ToList();
                model.DayInputs = AfterSaleCarKPIService.GetAfterSaleCustomerServiceDayInputs(y, m, d, model.Employees.Select(o => o.Id).ToArray());
                model.isconfirmed = model.DayInputs.Where(o => o.IsConfirmed).Count() > 0;
                return View(model);
            }
        }

        public ActionResult ModifyBackInputCustomerServiceDayInput(List<Domain.AfterSaleKpi.AfterSaleCustomerServiceDayInput> afterSaleCarlist, int y, int m, int d, bool isconfirm = false)
        { 
            using (var t = new Transtion())
            {
                t.Inject(EmployeeService);
                t.Inject(AfterSaleCarKPIService);
                DateTime now = new DateTime(y, m, d);
                var list = new List<S4Store.Domain.AfterSaleKpi.AfterSaleCustomerServiceDayInput>();
                foreach (var model in afterSaleCarlist)
                {
                    model.AddTime = now;
                    model.IsConfirmed = isconfirm;
                    list.Add(model);
                }
                AfterSaleCarKPIService.SubmitCustomerServiceDayInputsList(y, m, d, EmployeeService.GetEnableEmployee(Store.Id, EmployeeType.AccountSpecialist).ToList().Select(o => o.Id).ToArray(), list);
                t.Commit();
            }

            return new JsonResult
            {
                Data = new
                {
                    success = true
                }
            };
        }

    }
}
